package com.nineclock.system.controller;

import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.OrganizationControllerApi;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.OrganizationService;
import com.nineclock.system.service.SysCompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    SysCompanyUserService sysCompanyUserService;

    /**
     * 查询当前顶级部门
     * @return
     */
    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        List<Long> ids = organizationService.queryTopDepartmentIds();
        return Result.success(ids);
    }
    @Override
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentIds(
            @RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count = sysCompanyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }
    //根据部门ID查询子部门及员工列表
    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(
            @RequestParam(value = "id",required = false, defaultValue = "0") Long departmentId, Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> departmentOrUserSimpleDTOList = organizationService.querySimpleDepartment(departmentId, includeMember);
        return Result.success(departmentOrUserSimpleDTOList);
    }
    //获取全部员工
    @Override
    @RequestMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> getCompanyUserList() {
        List<SysCompanyUserDTO> ls=sysCompanyUserService.getCompanyUserList();
        return Result.success(ls);
    }
    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> getCompanyDepartmentList() {
        List<SysDepartmentDTO> list=organizationService.getCompanyDepartmentList();
        return Result.success(list);
    }

    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> getCompanyDepartmentUserList(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(value ="pageSize" ,required = false,defaultValue = "10") Integer pageSize,
            String keyword,
            @RequestParam(value = "departmentId",required = false,defaultValue = "0") Long departmentId) {
        PageResult<SysCompanyUserDTO> pageResult=organizationService.getCompanyDepartmentUserList(page, pageSize, keyword, departmentId);
        return Result.success(pageResult);
    }

    @Override
    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {
        sysCompanyUserService.parseExcel(excelFile);
        return Result.success();
    }
}
